#-------------------------------------------------------------------------------
# Copyright (c) 2022, Arm Limited. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
#-------------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.15)

if (NOT USE_KCONFIG_TOOL)
    return()
endif()

set(DOTCONFIG_FILE              "${CMAKE_BINARY_DIR}/.config")
set(ROOT_KCONFIG                "${CMAKE_SOURCE_DIR}/Kconfig")
set(PLATFORM_PATH               "${CMAKE_SOURCE_DIR}/platform/ext/target/${TFM_PLATFORM}")

find_package(Python3)

# Call the tfm_kconfig.py
execute_process(
    COMMAND
    ${Python3_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/kconfig/tfm_kconfig.py
    -k ${ROOT_KCONFIG} -o ${CMAKE_BINARY_DIR} -u gui -p ${PLATFORM_PATH}
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    RESULT_VARIABLE ret
)

if(NOT ret EQUAL 0)
    message(FATAL_ERROR "Kconfig tool failed!")
endif()

# Component configs generated by tfm_kconfig.py
if (EXISTS "${CMAKE_BINARY_DIR}/project_config.h")
    set(PROJECT_CONFIG_HEADER_FILE "${CMAKE_BINARY_DIR}/project_config.h"     CACHE STRING "User defined header file for TF-M config")
endif()

# Load project cmake configs generated by tfm_kconfig.py
if (EXISTS "${CMAKE_BINARY_DIR}/project_config.cmake")
    include("${CMAKE_BINARY_DIR}/project_config.cmake")
endif()
